******
* author: Anthony Harding (tony.harding@gatech.edu)
* Georgia Institute of Technology, School of Public Policy


* This script generates regression coefficients and tables exploring bounded exponential time trend
* Requires estout, estwide, ftools, ivreg2, reghdfe, and ivreghdfe packages

* 1st Imports Data
* 2nd Runs baseline regressions
******


clear all
set mem 1G
set matsize 10000
set maxvar 10000

* Set working directory
cd "\\Client\C$\Users\aharding6\Dropbox (GaTech)\Tobetransferred\ConsistentClimateConvergence - Copy\Replication"



*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------
* 1. Import and prep data
*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------

*-------------------------------------------------------------------------------
* Step 1 - Import main data
*-------------------------------------------------------------------------------
* Import main dataset (source Burke et al. 2015) - contains temperature and growth variables
use data/input/GrowthClimateDataset, clear

*-------------------------------------------------------------------------------
* End Step 1 - End Step 1 - End Step 1 - End Step 1 - End Step 1 - End Step 1 
*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
* Step 2 - Generate new useful variables
*-------------------------------------------------------------------------------
* Generate country indicator and xtset data
xtset iso_id year

* Make precip in micrometers
*replace UDel_precip_popweight = UDel_precip_popweight/1000
*replace UDel_precip_popweight_2 = UDel_precip_popweight_2/1000^2


* Generate new economic variables
qui gen poor = (GDPpctile_WDIppp<50)
qui replace poor=. if GDPpctile_WDIppp==.
qui gen log_gdpCAP_wdi = log(gdpCAP_wdi)
qui gen loggdpCAP_wdi_l = l.log_gdpCAP_wdi

* Generate new weather variables
qui gen UDel_temp_popweight_2 = UDel_temp_popweight^2
gen poor_temp = poor*UDel_temp_popweight
gen poor_prec = poor*UDel_precip_popweight
gen poor_temp2 = poor*UDel_temp_popweight_2
gen poor_prec2 = poor*UDel_precip_popweight_2
gen dtemp = UDel_temp_popweight-l.UDel_temp_popweight
gen dprec = UDel_precip_popweight-l.UDel_precip_popweight
gen dtemp2 = UDel_temp_popweight_2-l.UDel_temp_popweight_2
gen dprec2 = UDel_precip_popweight_2-l.UDel_precip_popweight_2
gen poor_dtemp = poor*dtemp
gen poor_dprec = poor*dprec
gen poor_dtemp2 = poor*dtemp2
gen poor_dprec2 = poor*dprec2

* Generate new year variables
gen timegr = time/(1+time)
gen timegrdiff = timegr-l.timegr
gen timegr2 = 1/2*time^2/(1+time)
*gen time2 = time^2
gen year2 = year^2
gen yeargr = year/(1+year)


gen timeex = time*exp(-1/50*(time))
gen yearex = year*exp(-1/50*(year-1960))
gen yearexdiff = yearex-l.yearex
gen timeexdiff = timeex-l.timeex

gen timeex2 = exp(-1/50*(time))
gen timegrb = time/50/(1+time/50)

*-------------------------------------------------------------------------------
* End Step 2 - End Step 2 - End Step 2 - End Step 2 - End Step 2 - End Step 2 
*-------------------------------------------------------------------------------

*-------------------------------------------------------------------------------
* Step 3 - Rename and relabel key variables
*-------------------------------------------------------------------------------
* Rename variables
ren UDel_temp_popweight temp
ren UDel_temp_popweight_2 temp2
ren UDel_precip_popweight prec
ren UDel_precip_popweight_2 prec2

* Relabel variables
label variable temp "Temp"
label variable temp2 "Temp Sq."
label variable prec "Precip"
label variable prec2 "Precip Sq."
label variable dtemp "$\Delta$Temp"
label variable dtemp2 "$\Delta$Temp Sq."
label variable dprec "$\Delta$Precip"
label variable dprec2 "$\Delta$Precip Sq."
label variable poor "poor"
label variable poor_temp "Temp*Poor"
label variable poor_prec "Precip*Poor"
label variable poor_temp2 "Temp Sq.*Poor"
label variable poor_prec2 "Precip Sq.*Poor"
*-------------------------------------------------------------------------------
* End Step 3 - End Step 3 - End Step 3 - End Step 3 - End Step 3 - End Step 3 
*-------------------------------------------------------------------------------



*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------
* 2. Run and Store Regressions
*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
* Step 1 - Pooled SR, bounded exponential country time trend, 1 GDP lag
*-------------------------------------------------------------------------------

** Estimate for different values of tau and store estimates in csv

forvalues x = 1(1)100 {
	replace timeex = time*exp((-time)/`x')
	qui ivreghdfe growthWDI temp temp2 prec prec2 (l.log_gdpCAP_wdi = l3.		log_gdpCAP_wdi), absorb(i.year  i.iso_id i.iso_id#c.time i.iso_id#c.timeex) cluster(iso_id)
	local name = "tau`x'"
	eststo `name'
}

esttab using "data/output/TauAnalysis_SR.csv",not noobs plain replace
eststo clear

*-------------------------------------------------------------------------------
* End Step 1 - End Step 1 - End Step 1 - End Step 1 - End Step 1 - End Step 1
*-------------------------------------------------------------------------------





*-------------------------------------------------------------------------------
* Step 2 - Pooled LR, convergent country time trend, 1 GDP lag
*-------------------------------------------------------------------------------

** Estimate for different values of tau and store estimates in csv

forvalues x = 1(5)100 {
	replace timeex = time*exp((-time)/`x')
	replace timeexdiff = timeex-l.timeex
	qui ivreghdfe growthWDI dtemp dtemp2 dprec dprec2 temp temp2 prec prec2 (l.growthWDI = l3.growthWDI), absorb(i.year  i.iso_id i.iso_id#c.timeexdiff) cluster(iso_id)
	local name = "tau`x'"
	eststo `name'
}

esttab using "data/output/TauAnalysis_LR.csv",not noobs plain replace
eststo clear

*-------------------------------------------------------------------------------
* End Step 2 - End Step 2 - End Step 2 - End Step 2 - End Step 2 - End Step 2
*-------------------------------------------------------------------------------








